package com.zmkm.controller.user;

import com.zmkm.common.ServerResponse;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IUserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/login/" })
public class LoginController {
	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = { "verify" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST }, produces = {
					"application/json;charset=utf-8" })
	public ModelAndView loginVerify(HttpServletRequest request, @RequestParam("userName") String userName,
			@RequestParam("passWord") String passWord) {
		ModelAndView mv = new ModelAndView();
		if (request.getSession().getAttribute("housekeeper") == null) {
			ServerResponse verify = this.iUserService.userNameAndpassWordVerify(userName, passWord);
			if (verify.getStatus() == 0) {
				Housekeeper data = (Housekeeper) verify.getData();
				request.getSession().setAttribute("housekeeper", data);
				mv.setViewName("index");
			} else {
				mv.addObject("serverResponse", verify);
				mv.setViewName("login");
			}
		} else {
			mv.setViewName("index");
		}
		return mv;
	}
}
